package com.example.myonlinemusic.controller;

import com.example.myonlinemusic.mapper.UserMapper;
import com.example.myonlinemusic.model.User;
import com.example.myonlinemusic.tools.Constant;
import com.example.myonlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    //注入userMapper
    @Resource
    private UserMapper userMapper;

    //传入参数进行登录
    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1(@RequestParam String username, @RequestParam String password, HttpServletRequest request){

        User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);

        //通过userMapper里面的登录方法进行登录
        User user = userMapper.login(userLogin);

        //登录成功会返回与一个对象，判断这个对象是不是空的，如果是空的表示登录失败，反之登录成功
        if(user != null){
            System.out.println("登录成功");
            //登录成功，还需要把登录的数据保存下来，保存到session里面
            request.setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功！",user);
        }else{
            System.out.println("登录失败");
            //登录失败，返回响应，将返回类型改成自定义的响应类型
            return new ResponseBodyMessage<>(-1,"登录失败！",userLogin);
        }
    }

    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    //传入参数进行登录
    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){

        /*User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);

        //通过userMapper里面的登录方法进行登录
        User user = userMapper.login(userLogin);*/

        //通过用户名来查询，看能否可以查询到
        User user = userMapper.selectByName(username);

        //登录成功会返回与一个对象，判断这个对象是不是空的，如果是空的表示登录失败，反之登录成功
        if(user != null){
//            System.out.println("登录成功");
            //此时就需要看这个用户名的密码和输入的密码是否一致
            boolean flg = bCryptPasswordEncoder.matches(password,user.getPassword());
            if(!flg){
                System.out.println("登录失败");
                return new ResponseBodyMessage<>(-1,"用户名或者密码错误！",user);
            }
            //登录成功，还需要把登录的数据保存下来，保存到session里面
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功！",user);
        }else{
            System.out.println("登录失败");
            //登录失败，返回响应，将返回类型改成自定义的响应类型
            return new ResponseBodyMessage<>(-1,"登录失败！",user);
        }
    }

    @RequestMapping("/logon")
    public ResponseBodyMessage<User> logon(@RequestParam String username, @RequestParam String password, HttpServletRequest request){

        String newPassword = bCryptPasswordEncoder.encode(password);
        User user = new User();
        //通过用户名来查询，看能否可以查询到
        user.setUsername(username);
        user.setPassword(newPassword);
        User u = userMapper.selectByName(username);
        if(u != null){
            //用户名已存在，不能注册
            return new ResponseBodyMessage<>(-1,"注册失败！用户名已存在",user);

        }
        int ret = userMapper.insertUser(user);

        if(ret == 1){

            return new ResponseBodyMessage<>(0,"注册成功！",user);
        }else{
            System.out.println("注册失败");
            //登录失败，返回响应，将返回类型改成自定义的响应类型
            return new ResponseBodyMessage<>(-1,"注册失败！",user);
        }
    }
}
